package PangModel;
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
/**
 * Created by Semih on 23.04.2015.
 */
public class GameMap
{
    public static final int GAME_PACE = 0;

    public static final int HERO_HEIGHT =50;
    public static final int HERO_WIDTH =50;

    public static final Dimension GAME_DIMENSION = new Dimension(500, 500);

    public static final int HERO_INDEX = 0;
    public static final int BALL_INDEX = 1;
    public static final int ANIMAL_INDEX = 2;
    public static final int BULLET_INDEX = 3;
    public static final int BARRIER_INDEX = 4;
    public static final int ITEM_INDEX = 5;
    public static final int WEAPON_INDEX = 6;

    private ArrayList<ArrayList> gameObjects; // PROBLEMATIC
    private ArrayList<Hero> heroes;
    private ArrayList<Ball> balls;
    private ArrayList<Animal> animals;
    private ArrayList<Bullet> bullets;
    private ArrayList<Barrier> barriers;
    private ArrayList<Item> items;
    private ArrayList<Weapon> weapons;

    private ImageStore imageStore = new ImageStore();

    public GameMap(int level)
    {
        gameObjects = new ArrayList<ArrayList>();

        heroes = new ArrayList<Hero>();
        balls = new ArrayList <Ball>();
        animals = new ArrayList<Animal>();
        bullets = new ArrayList<Bullet>();
        barriers = new ArrayList<Barrier>();
        items = new ArrayList <Item>();
        weapons = new ArrayList<Weapon>();

        gameObjects.add(HERO_INDEX, heroes);
        gameObjects.add(BALL_INDEX, balls);
        gameObjects.add(ANIMAL_INDEX, animals);
        gameObjects.add(BULLET_INDEX, bullets);
        gameObjects.add(BARRIER_INDEX, barriers);
        gameObjects.add(ITEM_INDEX, items);
        gameObjects.add(WEAPON_INDEX, weapons);
        // then set the game according to the levels

        if(level == 1)
        {
            Hero h = null;
            h = new Hero();

            h.setCoordinates((double) GAME_DIMENSION.getWidth() / 2 - HERO_WIDTH+120, (double) GAME_DIMENSION.getHeight() - HERO_HEIGHT - 5);
            heroes.add(h);

            Weapon w = new Hook();
            weapons.add(w);

            Ball b;
            b = new Ball(2, GameObject.BALL_SPLITTED_DIRECTION_LEFT, new Point((int) GAME_DIMENSION.getWidth() / 2, (int) GAME_DIMENSION.getHeight() / 2));
            balls.add(b);

            b = new Ball(3, GameObject.BALL_SPLITTED_DIRECTION_RIGHT, new Point((int) GAME_DIMENSION.getWidth() / 2, (int) GAME_DIMENSION.getHeight() / 2));
            balls.add(b);

            Barrier middle = null;
            middle = new Barrier(10,(int) GameMap.GAME_DIMENSION.getHeight()-250);
            middle.setCoordinates(300, 200);
            barriers.add(middle);

        }
        createBarriers();

    }

    private void createBarriers()
    {
        Barrier left = null;
        left = new Barrier(5,(int) GameMap.GAME_DIMENSION.getHeight()+25);
        left.setCoordinates(-5, 0);
        Barrier right = null;
        right = new Barrier(5, (int) GameMap.GAME_DIMENSION.getHeight()+25);
        right.setCoordinates((int) GameMap.GAME_DIMENSION.getWidth()-1, 0);
        Barrier top = null;
        top = new Barrier((int) GameMap.GAME_DIMENSION.getWidth(),5);
        top.setCoordinates(0, -5);
        Barrier bottom = null;
        bottom = new Barrier((int) GameMap.GAME_DIMENSION.getWidth()+50, 5);
        bottom.setCoordinates(-25, (int) GameMap.GAME_DIMENSION.getHeight() - 1);

        barriers.add(left);barriers.add(right);
        barriers.add(top);barriers.add(bottom);
    }

    public void moveHero(int direction) throws UnsupportedArgumentException
    {
        if (direction == GameObject.LEFT)
        {
            Hero h = heroes.get(0);
            Point p = h.getCoordinates();

            if (p.getX() < 1)
                return;

            h.move(GameObject.LEFT);
        }
        else if (direction == GameObject.RIGHT)
        {
            Hero h = heroes.get(0);
            Point p = h.getCoordinates();

            if (p.getX()  + h.getWidth() > GAME_DIMENSION.getWidth() -1)
                return;

            h.move(GameObject.RIGHT);
        }
    }

    public void shoot()
    {
        Hero hero = heroes.get(0);
        if (!hero.canShoot())
            return;

        boolean isThereBulletsOnAir = !bullets.isEmpty();

        Weapon weap = weapons.get(0);
        ArrayList<Bullet> newBullets = weap.shoot(heroes.get(0).getCoordinates(), isThereBulletsOnAir);
        bullets.addAll(newBullets);
    }

    public ArrayList<ArrayList> getGameObjects()
    {
        return gameObjects;
    }

    public void drawObjects(Graphics page)
    {
        for (ArrayList<GameObject> al: gameObjects)
            for (GameObject go: al)
                go.draw(page);
    }
}
